package com.stjb.furns.web;

import com.google.gson.Gson;
import com.stjb.furns.entity.Member;
import com.stjb.furns.service.MemberService;
import com.stjb.furns.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * 该Servlet处理和Member相关的请求
 *
 * @Author: situ
 * @Date: 2025/5/26 15:26
 * @Description:
 **/
public class MemberServlet extends BasicServlet {

    private MemberService memberService = new MemberServiceImpl();

    //protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //
    //    //获取action
    //    String action = request.getParameter("action");
    //    //System.out.println(action);
    //    if ("login".equals(action)) {
    //        login(request, response);
    //    } else if ("register".equals(action)) {
    //        register(request, response);
    //    }else {
    //        response.getWriter().write("请求参数有问题");
    //    }
    //
    //}

    /**
     * 处理会员登录
     * 记录保存到session中 仅单次会话有效
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //System.out.println("ok");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //System.out.println(username);
        //System.out.println(password);
        //判断是否登录成功
        Member member = memberService.login(username, password);
        if (member != null) {
            //将登录对象放入session
            request.getSession().setAttribute("member", member);
            //System.out.println("登录成功");
            //跳转登录成功页面
            request.getRequestDispatcher("/views/member/login_ok.jsp").forward(request, response);
        } else {
            //跳转登录失败页面
            //把登陆错误信息放到request域
            request.setAttribute("msg_login", "用户名或者密码错误");
            request.setAttribute("username", username);
            //System.out.println("登录失败");
            request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
        }
    }

    /**
     * 处理会员注册
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //System.out.println("ok");
        //接收用户信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        //获取前端用户填写的提交的验证码
        String code = request.getParameter("code");

        //从session中获取验证码
        String token = (String) request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //立即删除验证码数据 防止重复提交
        request.getSession().removeAttribute(KAPTCHA_SESSION_KEY);

        //如果用户提交的验证码为真则验证通过
        //token.equalsIgnoreCase(code) 忽略大小写
        if (token != null && token.equalsIgnoreCase(code)) {

            //用户名是否可用
            if (!memberService.isExistsUsername(username)) {
                //z注册
                //构建member
                Member registerMember = new Member(null, username, password, email);
                if (memberService.registerMember(registerMember)) {
                    //System.out.println("注册成功");
                    //请求转发
                    request.getRequestDispatcher("/views/member/register_ok.jsp").forward(request, response);
                } else {
                    //System.out.println("注册失败");
                    request.getRequestDispatcher("/views/member/register_fail.jsp").forward(request, response);

                }
                //System.out.println("用户名 " + username + " 不存在 可以注册");
            } else {
                //返回注册页面
                //System.out.println("用户名 " + username + " 存在 不可以注册");
                request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
            }
        } else {//验证码不正确
            request.setAttribute("msg", "验证码不正确");
            request.setAttribute("active", "register");
            request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
        }
    }


    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //销毁session 销毁所有session 包括购物车
        req.getSession().invalidate();
        //重定向到网站首页
        resp.sendRedirect(req.getContextPath());
    }

    /**
     * 验证用户名是否存在 以Json格式发给前端
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void UserNaisExistme(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取用户名
        String username = req.getParameter("username");
        //调用
        boolean isExistsUsername = memberService.isExistsUsername(username);
        //构建Json
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("isExist",isExistsUsername);
        Gson gson = new Gson();
        String resultGson = gson.toJson(resultMap);

        //发送到前端
        resp.getWriter().write(resultGson);


    }
}
